// 性别
var forSex;
//当前房间对象
var roomObj
//当前房间集合
var roomList
//当前班级对象
var classObj
//当前班级集合
var classList
window.onload=function (){
    var info= getQuery("errorInfo");
    if (info=="noland"){
        $("errorDiv").innerHTML="对不起，请先登录"
    }
    findAll(1);
    findBySex();
    findClass();
}
function changeFace(){
    var reader = new FileReader();//定义文件读取流对象
    reader.readAsDataURL(document.getElementById("fileTxt").files[0]);
    reader.onload = function (ev) {
        $("faceImg").src = ev.target.result;
    }
}
function findAll(pageNO) {
    axios.get("/stu/findByItem",{
        params: {
            pageNO,
            stuName:$("nameTxt").value,
            className:$("classTxt").value,
            roomAddress:$("roomTxt").value
        }
    }).then(resp=>{
        let info =resp.data;

        let str="";
        info.list.forEach(n=>{
            str+=`<tr><td>${n.stuName}</td><td>${n.sex}</td><td>${n.stuClass.className}</td><td>${n.stuRoom.roomAddress}</td><td>${n.stuPhone}</td><td><input type="button" value="学生退房" onclick="delStu(${n.stuId})"><input type="button" value="学生换房" onclick="changeRoom(${n.stuId})"><input type="button" value="详细信息" onclick="findStu(${n.stuId})"><td></tr>`
        });
        $("data").innerHTML=str;
        var cutStr = "";
        for(var i=1;i<=info.totalPage;i++){
            cutStr += `<a href="javascript:findAll(${i})" style="margin-left: 10px;text-decoration: none">${i}</a>`
        }

        $("cutDiv").innerHTML = cutStr;
    })
}

 function add(){
    $("addDiv").style.visibility="visible";

}

function delStu(stuId){
    axios.get("/stu/delStu",{
        params:{
            stuId
        }
    }).then(resp=>{
        var info = resp.data;
        if (info=="ok"){
            findAll(1);
        }
    })
}

async function changeRoom(stuId){
    $("changeDiv").style.visibility="visible";
    await axios.get("/stu/findById",{
        params:{
            stuId,
        }
    }).then(resp=>{
        var info = resp.data;
        forSex=info.sex;
        $("id").value=info.stuId;
        $("changeName").innerHTML=info.stuName;
        $("changeSex").innerHTML=info.sex;
        $("changeClass").innerHTML=info.stuClass.className;
        $("changeRoom").innerHTML=info.stuRoom.roomAddress;

        $("facChangeImg").src="/stuFace/"+info.imgPath;
    })
    axios.get("/room/findBySex",{
        params: {
            sex:forSex,
        }
    }).then(resp=>{
        var info = resp.data;
        info.filter(n => n.maxNum > n.nowNum);
        roomList = info;
        str=""
        roomList.forEach(n=>{
            str+=`<option value="${n.roomId}">${n.roomAddress}</option>`
        })
        roomObj = roomList.find(n => n.roomId = info);

        $("changeRooms").innerHTML=str;
    })

}

function changeRooms(){
    axios.get("/stu/changeRoom",{
        params:{
            stuId:$("id").value,
            roomId:$("changeRooms").value
        }
    }).then(resp=>{
        var info = resp.data;
        if (info=="ok"){
            back()
            findAll(1)
        }
    })
}
async function addStu(){

    var formObj= new FormData();
    formObj.append("uploadImg",$("fileTxt").files[0]);
    let config={
        headers :{'Content_Type':'multipart/form-data' }
    }
   await axios.post("/upload",formObj,config).then(resp=>{
        uploadFileName=resp.data;
    });


     axios.get("/stu/addStu",{
         params:{
             stuName: $("addName").value,
             sex: forSex,
             stuPhone:$("addPhone").value,
             imgPath:uploadFileName,
             roomId:roomObj.roomId,
             classId:classObj.classId,
             classname:classObj.className,
             address:roomObj.roomAddress
         }
     }).then(resp=>{
         let info = resp.data;
         if (info=="ok"){
             back()
             findAll(1);
         }
     })
}

function chooseRoom(){
    var info = $("roomSelect").value;
    roomObj = roomList.find(n => n.roomAddress == info);
}

function chooseClass(){
    var info = $("classSelect").value;
    classObj = classList.find(n => n.className == info);
}

function findBySex(){
    let info =document.getElementsByName("sex");

    for (let i = 0; i <info.length ; i++) {
        if (info[i].checked){
            forSex=info[i].value;
        }
    }

    axios.get("/room/findBySex",{
        params: {
            sex:forSex,
        }
    }).then(resp=>{
        var info = resp.data;

        info.filter(n => n.maxNum > n.nowNum);
        roomList = info;
        let str="";
        info.forEach(n=>{
            str+=`<option value="${n.roomAddress}">${n.roomAddress}</option>`
        })
        $("roomSelect").innerHTML=str;
    })
}

function findClass(){

    axios.get("/class/findAll").then(resp=>{
        let info = resp.data;
        classList=info;
        let str;
        info.forEach(n=>{
            str+= `<option>${n.className}</option>`
        })
        $("classSelect").innerHTML=str;
    })
}

function back(){
    $("addDiv").style.visibility="hidden";
    $("infoDiv").style.visibility="hidden";
    $("changeDiv").style.visibility="hidden"
}
var allStuId;
function findStu(stuId){
    $("infoDiv").style.visibility="visible";
    axios.get("/stu/findById",{
        params:{
            stuId
        }
    }).then(resp=>{
        let info =resp.data;
        allStuId=stuId;
        $("nameSpan").innerHTML=info.stuName;
        $("sexSpan").innerHTML=info.sex;
        $("classSpan").innerHTML=info.stuClass.className;
        $("roomSpan").innerHTML=info.stuRoom.roomAddress;
        $("dateSpan").innerHTML=info.stuDate;
        $("phoneSpan").innerHTML=info.stuPhone;
        $("facInfoImg").src="/stuFace/"+info.imgPath;
    })
}